package com.subsequent.method;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsequent.dto.UserDto;
import com.subsequent.mapper.UsersMapper;
import com.subsequent.pojo.Users;
import com.subsequent.utils.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("codeLoginStrategy")
public class CodeLoginStrategy implements LoginStrategy{
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public Result login(UserDto dto) {
        if(dto==null){
           return Result.error("参数错误");
        }
        QueryWrapper<Users>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Users::getPhone,dto.getPhone());
        Users users=usersMapper.selectOne(queryWrapper);
        if(users!=null){
            Integer userCode = (Integer) redisTemplate.opsForValue().get("UserCode1:");
            if(userCode.equals(dto.getCode())){
                return Result.success("登录成功");
            }
            return Result.error("验证码错误");
        }
        return Result.error("手机号不存在");
    }
}
